package com.ql.mangersystem.mapper;

import com.ql.mangersystem.entity.po.File;
import com.ql.mangersystem.entity.vo.FileVo;
import com.ql.mangersystem.entity.vo.RecycleVo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Entity com.ql.mangersystem.entity.po.File
 */
@Mapper
public interface FileMapper {
    @Insert("insert into `file`(original_name,now_name,upload_time,download_time,delete_time,uid,url) " +
            "values (#{originalFilename},#{newName},#{now},#{now},#{now},#{userId},#{url})")
    int insert(@Param("originalFilename") String originalFilename,@Param("newName") String newName,@Param("now") LocalDateTime now,@Param("url")String url,@Param("userId")Integer userId);

    @Select("select id from `file` where now_name = #{newName} and deleted=1")
    Integer selectByNewName(String newName);

    @Select("select * from `file` where id = #{id} and deleted =1")
    File selectById(Integer id);

    @Select("select id,original_name,upload_time,url from `file`  where uid = #{userId} and deleted =1 order by upload_time desc limit #{skipNum}, #{pageSize}")
    List<FileVo> selectByPage(@Param("skipNum") Integer skipNum,@Param("pageSize") Integer pageSize, @Param("userId") Integer userId);

    @Select("select count(id) from `file` where uid = #{userId} and deleted =1")
    Integer selectCount(Integer userId);

    @Update("update `file` set deleted = 0 ,delete_time=#{time} where id = #{id}")
    int logicDelete(@Param("id") Integer id,@Param("time") LocalDateTime time);

    @Update("update `file` set original_name = #{name},upload_time=#{now} where id =#{id} and deleted =1")
    int rename(@Param("id") Integer id, @Param("name") String name,@Param("now")LocalDateTime now);

    @Select("select id,original_name,upload_time,url from `file`  where uid = #{userId} and deleted =1 and original_name like concat('%',#{fileName},'%')  order by upload_time desc limit #{skipNum}, #{pageSize}")
    List<FileVo> selectByName(@Param("skipNum") Integer skipNum,@Param("pageSize") Integer pageSize, @Param("userId") Integer userId,@Param("fileName") String fileName);

    @Select("select count(id) from `file` where uid =#{userId} and deleted =1 and original_name like concat('%',#{fileName},'%') ")
    Integer CountByName(@Param("userId") Integer userId,@Param("fileName") String fileName);

    int logicDelBatch(@Param("ids") List<Integer> ids,@Param("time") LocalDateTime time);

    @Select("select id,original_name,delete_time,url from `file`  where uid = #{userId} and deleted =0 order by upload_time desc limit #{skipNum}, #{pageSize}")
    List<RecycleVo> recycleByPage(@Param("skipNum") Integer skipNum, @Param("pageSize") Integer pageSize, @Param("userId") Integer userId);

    @Select("select count(id) from `file` where uid = #{userId} and deleted =0")
    Integer recycleCount(Integer userId);

    @Delete("delete from `file` where id =#{id}")
    int delete(Integer id);

    @Select("select * from `file` where id = #{id} and deleted =0")
    File selectByDeleteId(Integer id);

    @Update("update `file` set deleted = 1,upload_time=#{time} where id = #{id}")
    int recover(@Param("id") Integer id,@Param("time") LocalDateTime time);

    @Select("select id,original_name,delete_time,url from `file`  where uid = #{userId} and deleted =0 and original_name like concat('%',#{fileName},'%')  order by delete_time desc limit #{skipNum}, #{pageSize}")
    List<RecycleVo> recycleByName(@Param("skipNum") Integer skipNum,@Param("pageSize") Integer pageSize, @Param("userId") Integer userId,@Param("fileName") String fileName);

    int recoverAll(@Param("ids") List<Integer> ids,@Param("time") LocalDateTime time);

    int deleteAll(List<Integer> ids);

}




